package ICM_OCSF;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JFrame;

import ClientServer.ProtocolIF;
import ClientServer.ServerSide;
import Entity.Message;
import GUI.ServerConsoleGUI;
import ServerOperation.DBHandler;
import ServerOperation.DailyUpdates;
import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;

public class Server extends AbstractServer {

	DBHandler dbHandle;
	static final  int DEFAULT_PORT = 6001; 
	
	private boolean ServerAlive;
	
	public Server(int port) {
		super(port);
		ServerAlive=true;
		// TODO Auto-generated constructor stub
	}
	/**
	 * this method returns if server is alive or not
	 * @return true or flase 
	 */
	public boolean isAlive()
	{
		return ServerAlive;
	}
	
	  
	 
	@Override
	
	 /*
	  * (non-Javadoc)
	  * @see ocsf.server.AbstractServer#handleMessageFromClient(java.lang.Object, ocsf.server.ConnectionToClient)
	  */
	protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
		// TODO Auto-generated method stub
		System.out.println("Server : handleMessageFromClient ");
		if ((msg instanceof Message)==true)
		{
		
			Message AnswerMessage =  dbHandle.messageHandle((Message)msg);
			if (AnswerMessage!=null)
				System.out.println(AnswerMessage.toString());
			else
				System.out.println("null message");
			
			try {
				client.sendToClient(AnswerMessage);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			try {
				client.sendToClient(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see ocsf.server.AbstractServer#serverStarted()
	 */
	protected void serverStarted() {
		// TODO Auto-generated method stub
		
		dbHandle = DBHandler.getInstance();
		dbHandle.disconnectAllUser();
		DailyUpdates du = new DailyUpdates(dbHandle.getConn(),this);
	//	new Thread(du).start();
		
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see ocsf.server.AbstractServer#serverStopped()
	 */
	protected void serverStopped() {
		// TODO Auto-generated method stub
		super.serverClosed();
		dbHandle.disconnectAllUser();
		System.out.println("Disconnectd all users");
		dbHandle= null;
		ServerAlive=false;
	}

	@Override
	/**
	 *  opretions done when server is closed
	 */
	protected void serverClosed() 
	{
		// TODO Auto-generated method stub
		super.serverClosed();
		dbHandle.disconnectAllUser();
		System.out.println("Disconnectd all users");
		dbHandle= null;
		ServerAlive=false;
	}
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#finalize()
	 */
	protected void finalize() 
	{
		this.serverClosed();
		ServerAlive=false;
	}
	
	/**
	 * main function runs the server
	 * @param args
	 */
	public static void main(String[] args) 
	{
	  int port = 6001; //Port to listen on

	  
	    port =  DEFAULT_PORT; //Set port to 6001
	     Server sv = new Server(port);
	
	  try 
	  {
	    sv.listen(); //Start listening for connections
	    System.out.println(" listen");
	  } 
	  catch (Exception ex) 
	  {
	    //System.out.println("ERROR - Could not listen for clients!");
		 System.out.println("Could not listen");
	  }
	  
	}
}
